//
//  HomeDailyView.m
//  jingdongfang
//
//  Created by LJL on 2018/5/21.
//  Copyright © 2018年 BOE. All rights reserved.
//

#import "HomeDailyView.h"
#import "UIImageView+LBBlurredImage.h"

@interface HomeDailyView (){
    UIImageView * leftView;
    
    UILabel *monthL;
    UILabel *weekdayL;
    UIView *lineV;
    UILabel *yearL;
    UILabel *dayL;
    UILabel *textL;
}

@property (nonatomic, strong) UIImageView *bgImgView;
@property (nonatomic, strong) UIImageView * rightView;

@end

@implementation HomeDailyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    UIImage *iconImage = [UIImage imageNamed:@"个人主页bj1"];//默认图片
    //背景
    self.backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -NavigationMagin- KHeadViewOff, self.bounds.size.width,
                                                                         self.bounds.size.height + NavigationMagin+KHeadViewOff)];
    [_backgroundImageView setImageToBlur:iconImage blurRadius:20 completionBlock:nil]; //高斯处理
    [self addSubview:_backgroundImageView];
    _backgroundImageView.contentMode = UIViewContentModeScaleToFill;
    
    leftView = [[UIImageView alloc] init];
    leftView.image = [UIImage imageNamed:@"日历1"];
    [self addSubview:leftView];
   
    
    self.rightView = [[UIImageView alloc] init];
    _rightView.backgroundColor = [UIColor mainRandomColor];
//    rightView.image = [UIImage imageNamed:@"1"];
    _rightView.layer.cornerRadius = 5.0f;
    _rightView.layer.masksToBounds = YES;
    _rightView.contentMode = UIViewContentModeScaleToFill;
    [self addSubview:_rightView];

    
    // 获取代表公历的NSCalendar对象
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:dt];
    
    monthL = [[UILabel alloc] init];
    monthL.textAlignment = NSTextAlignmentCenter;
    monthL.textColor = [UIColor c3Color];
    monthL.font = [UIFont boldSystemFontOfSize:18];
    NSString *str;
    switch (comp.month) {
        case 1:
            str = @"Jan";
            break;
        case 2:
            str = @"Feb";
            break;
        case 3:
            str = @"Mar";
            break;
        case 4:
            str = @"Apr";
            break;
        case 5:
            str = @"May";
            break;
        case 6:
            str = @"Jun";
            break;
        case 7:
            str = @"Jul";
            break;
        case 8:
            str = @"Aug";
            break;
        case 9:
            str = @"Sep";
            break;
        case 10:
            str = @"Oct";
            break;
        case 11:
            str = @"Nov";
            break;
        case 12:
            str = @"Dec";
            break;
            
        default:
            break;
    }
    monthL.text = str;
    [leftView addSubview:monthL];
    
    
    weekdayL = [[UILabel alloc] init];
    weekdayL.textAlignment = NSTextAlignmentCenter;
    weekdayL.textColor = [UIColor c3Color];
    weekdayL.backgroundColor = [UIColor c7Color];
    weekdayL.layer.cornerRadius = 7.5;
    weekdayL.layer.masksToBounds = YES;
    weekdayL.font = [UIFont boldSystemFontOfSize:10];
    NSString *str1;
    switch (comp.weekday) {
        case 2:
            str1 = @"一";
            break;
        case 3:
            str1 = @"二";
            break;
        case 4:
            str1 = @"三";
            break;
        case 5:
            str1 = @"四";
            break;
        case 6:
            str1 = @"五";
            break;
        case 7:
            str1 = @"六";
            break;
        case 1:
            str1 = @"日";
            break;
            
        default:
            break;
    }
    weekdayL.text = [NSString stringWithFormat:@"星期%@", str1];
    [leftView addSubview:weekdayL];
   
    lineV = [[UIView alloc] init];
    lineV.backgroundColor = [UIColor c3Color];
    [leftView addSubview:lineV];

    yearL = [[UILabel alloc] init];
    yearL.textAlignment = NSTextAlignmentCenter;
    yearL.textColor = [UIColor c3Color];
    yearL.font = [UIFont boldSystemFontOfSize:11];
    yearL.text = [NSString stringWithFormat:@"%ld",comp.year];
    [leftView addSubview:yearL];
    
    dayL = [[UILabel alloc] init];
    dayL.textAlignment = NSTextAlignmentCenter;
    dayL.textColor = [UIColor c3Color];
    dayL.font = [UIFont boldSystemFontOfSize:39];
    dayL.text = [NSString stringWithFormat:@"%ld",comp.day];
    [leftView addSubview:dayL];
    
    textL = [[UILabel alloc] init];
    textL.textAlignment = NSTextAlignmentLeft;
    textL.textColor = [UIColor c3Color];
    textL.font = [UIFont systemFontOfSize:12];
    textL.text = NSLocalizedString(@"home18", nil);
    [self addSubview:textL];

}

#pragma mark - 处理图片的方法  网络请求回来后调用
- (void)setIconPicStr:(NSString *)iconPicStr withCompleteBlock:(picCompleteBlock) completion {
    __weak typeof(self) weakSelf = self;
            //回调这里
            if (iconPicStr)
            {
                [_rightView sd_setImageWithURL:[NSURL URLWithString:iconPicStr] placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL)
                 {
                     if (image)
                    {
                        [weakSelf.backgroundImageView setImageToBlur:image blurRadius:20 completionBlock:^{
                            completion();
                        }];
                     }
                 }];
            }
            else
            {
                UIImage *iconImage = [UIImage imageNamed:@"个人主页bj1"]; //换成sd
                _rightView.image = iconImage;
                [weakSelf.backgroundImageView setImageToBlur:iconImage blurRadius:20 completionBlock:^{
                    completion();
                }]; //高斯处理
            }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    @KWeakObj(self)
    @KWeakObj(leftView)
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(GSCALE(31));
        make.top.mas_offset(91 - NavigationMagin);
        make.width.mas_offset(GSCALE(104));
        make.height.equalTo(leftViewWeak.mas_width).multipliedBy(116 / 104.f);
    }];
    
    [_rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftViewWeak.mas_right).offset(-2);
        make.width.mas_offset(GSCALE(210));
        make.height.equalTo(_rightView.mas_width).multipliedBy(110 / 210.f);
        make.centerY.mas_equalTo(leftViewWeak).offset(GSCALE(3));
    }];
    
    [monthL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_equalTo(GSCALE(10));
        make.height.mas_equalTo(GSCALE(20));
    }];
    
    [weekdayL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(monthL.mas_bottom).offset(5);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(GSCALE(15));
        make.centerX.mas_equalTo(leftViewWeak);
    }];
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(1);
        make.centerY.mas_equalTo(weekdayL);
    }];
    [leftView bringSubviewToFront:weekdayL];
    
    [yearL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(GSCALE(15));
        make.bottom.mas_equalTo(-GSCALE(5));
    }];
    
    [dayL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(weekdayL.mas_bottom).offset(0);
        make.bottom.mas_equalTo(yearL.mas_top).offset(0);
    }];
    
    [textL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(leftView);
        make.top.mas_equalTo(leftView.mas_bottom).offset(GSCALE(10));
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(12);
    }];
    [self bringSubviewToFront:leftView];
    
    self.userInteractionEnabled = YES;
    _backgroundImageView.userInteractionEnabled = YES;
    leftView.userInteractionEnabled = YES;
    _rightView.userInteractionEnabled = YES;
    _rightView.multipleTouchEnabled = YES;
    
    [_rightView setTapActionWithBlock:^{
        if (selfWeak.block)
        {
            selfWeak.block();
        }
    }];
}
@end
